package com.teamwings.ctrls.formCtrls;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.teamwings.common.aspect.annotation.FormControl;
import com.teamwings.common.constant.FormConstant;
import com.teamwings.common.vo.Result;
import com.teamwings.ctrls.FormComponent;
import com.teamwings.modules.design.vo.FormVo;
import com.teamwings.modules.page.service.IPageService;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 根据部门选择用户。这个组件可以存储多个id，所以有可能会出现长度不够的情况
 */
@EqualsAndHashCode(callSuper = true)
@FormControl(type = "choose_user_by_dept")
@Data
public class ChooseUserByDept extends FormComponent {

//    @Autowired
//    private ISysUserServiceFeign sysUserServiceFeign;
//    @Autowired
//    private ISysDepartServiceFeign sysDepartServiceFeign;

    public ChooseUserByDept() {
        this.type = "choose_user_by_dept";
        this.fieldDefaultLength = "50";
        this.fieldType = "VARCHAR";
    }


    /**
     * choose_user_by_dept 相关方法
     * 1. 根据部门id获取用户
     * 2. 根据用户账号 获取用户列表 getUserList
     * 3. 获取部门树
     *
     * @param type
     * @param pageService
     * @param formDesignCode
     * @param paramsData
     * @return
     */
    @Override
    public Result<?> trigger(
            FormConstant.TRIGGER_TYPE type,
            IPageService pageService,
            FormVo formDesignCode,
            Object paramsData
    ) {
        JSONObject jo = JSON.parseObject(paramsData + "");
        // 获取人员的数据
        String accounts = jo.getString("accounts");
        List<String> listAccounts = Arrays.asList(accounts.split(","));

        String sql = "SELECT id, username, realname, email, phone, org_code, work_no, post " +
                "FROM sys_user WHERE del_flag = 0 AND username in (:ids)";

        Map<String, Object> params = new HashMap<>();
        params.put("ids", listAccounts);

        JdbcTemplate jdbcTemplate = pageService.getJdbcTemplate();
        NamedParameterJdbcTemplate namedParameterJdbcTemplate =
                new NamedParameterJdbcTemplate(jdbcTemplate.getDataSource());

        List<Map<String, Object>> result = namedParameterJdbcTemplate.queryForList(sql, params);
        return Result.OK(result);

//        JSONObject jo = JSON.parseObject(paramsData + "");
//        Integer listType = jo.getInteger("listType");

//        if (type == FormConstant.TRIGGER_TYPE.CTRL_HANDLER) {
//            if (listType.equals(0)) {
//                // 根据部门id获取用户
//                String id = jo.getString("id");
//                String realname = jo.getString("realname");
//
//                String jsonString = sysUserServiceFeign.queryUserByDepId(id, realname);
//                return JSON.parseObject(jsonString, Result.class);
//
//            } else if(listType.equals(1)) {
//                // 根据用户账号 获取用户列表 getUserList
//                String username = jo.getString("username");
//                String pageNo = jo.getString("pageNo");
//                String pageSize = jo.getString("pageSize");
//
//                SysUser sysUser = new SysUser();
//                sysUser.setUsername(username);
//                String jsonString = sysUserServiceFeign.queryPageList(sysUser, pageNo, pageSize);
//
//                return JSON.parseObject(jsonString, Result.class);
//            } else if(listType.equals(2)) {
//                // 获取部门树
//                String id = jo.getString("id");
//                String realname = jo.getString("realname");
//
//                String jsonString = sysDepartServiceFeign.queryTreeList(id, realname);
//                return JSON.parseObject(jsonString, Result.class);
//            }
//        }
//         return Result.error("参数有误，查询失败。");
    }
}
